const moment = require("../libs/moment.min.js");
moment.locale('en', {
  longDateFormat : {
      l: "YYYY-MM-DD",
      L: "YYYY-MM-DD HH:mm"
  }
});

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//返回日期时间格式为 yyyy-mm-dd hh:mm:ss
const timeFormat=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return year + '-' + month + '-' + day + ' ' + hour + ':' + m + ':' + s
}

//返回日期时间格式为 yyyy-mm-dd
const timeFormatDay=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();    
  return year + '-' + month + '-' + day
}

//返回日期时间格式为 yyyy-mm-dd HH
const timeFormatDayMin=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();    
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  return year + '-' + month + '-' + day + ' ' + hour + ':' + m
}

//返回周几
const timeFormatWeek=(time) =>{
  let date = new Date(time);  
  let day = date.getDay(); 
  let week='';
  switch(day){
    case 1:
      week='周一';
      break;
    case 2:
      week='周二';
      break;
    case 3:
      week='周三';
      break;
    case 4:
      week='周四';
      break;
    case 5:
      week='周五';
      break;
    case 6:
      week='周六';
      break;
    case 0:
      week='周日';
      break;   
  }

  return week; 
}

/**
 * 计算某个日期几天前的日期
 * date:是个日期类型
 * n:天数
 * 返回的也是日期类型
 */
const countPrevDay=(date,n) =>{   
  let targetday_milliseconds=date.getTime() - 1000*60*60*24*n;
  date.setTime(targetday_milliseconds);    
  let year,month,day;
  year = date.getFullYear();
  month = date.getMonth();
  day = date.getDate() ;
  let rusult=new Date(year,month,day).getTime();   
  return rusult; 
}

/**
 * 计算某个日期几天后的日期
 * date:是个日期类型
 * n:天数
 * 返回的也是日期类型
 */
const countNextDay=(date,n) =>{   
  let targetday_milliseconds=date.getTime() + 1000*60*60*24*n;
  let rusult=new Date(targetday_milliseconds);   
  return rusult;
}

//时间转化
const secondToFormat=(value) =>{
  //需要转换的时间-秒
  let theTime = parseInt(value); 
  //分
  let theTime1 = 0;
  //小时
  let theTime2 = 0; 
  //天
  let theTime3 = 0;

  if (theTime >= 60) {
      theTime1 = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
      if (theTime1 >= 60) {
          theTime2 = parseInt(theTime1 / 60);
          theTime1 = parseInt(theTime1 % 60);
          if (theTime2 > 24) {
              //大于24小时
              theTime3 = parseInt(theTime2 / 24);
              theTime2 = parseInt(theTime2 % 24);
          }
      }
  }
  
  let result = '';
  if (theTime > 0) {
      result = "" + parseInt(theTime) + "秒";
  }
  if (theTime1 > 0) {
      result = "" + parseInt(theTime1) + "分" + result;
  }
  if (theTime2 > 0) {
      result = "" + parseInt(theTime2) + "小时" + result;
  }
  if (theTime3 > 0) {
      result = "" + parseInt(theTime3) + "天" + result;
  }
  return result;
}

//返回日期时间格式为 yyyy年m月
const timeFormatMonth=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;  
  return year + '年' + month + '月'
}

//返回前n个月的日期（返回的是个日期对象）
const beforeMonth=(n) =>{
  let myDate = new Date();
  let year = myDate.getFullYear();
  let month = myDate.getMonth()+1; 

  let ylow=parseInt(n/12);
  let mlow=parseInt(n%12);

  if(ylow>0){
    year-=ylow;
  }else{
    year=year;
  }

  if((mlow>month) || (mlow==month)){
    year=year-1;
    month=12-mlow+month;     
  }else{
    month-=mlow;    
  }
  return new Date(year,month,1);
}

//验证手机号格式
let checkPhoneFormat=(phone)=>{
  let reg = /^1[3-9]\d{9}$/;    
  if (reg.test(phone)){
    return true;
  }else{
    return false;
  }
}

/**
 * 去重数组相加
 * 
 */
const uniqueConcat=(array1,array2)=>{
  //遍历当前数组
  for (var i = 0; i < array2.length; i++) {
    if (array1.indexOf(array2[i]) == -1) array1.push(array2[i]);
  }
  return array1;
}

//时间格式化成UTC形式
const timeFormatTZ=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return year + '-' + month + '-' + day + 'T' + hour + ':' + m + ':' + s+'.000Z'
}

//返回日期时间格式为 yyyy-mm
const timeFormatMonth2=(time) =>{
  let date = new Date(time);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  return year + '-' + month
}

//返回日期时间格式为 hh:mm:ss
const timeFormatTime=(time) =>{
  let date = new Date(time);  
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return hour + ':' + m + ':' + s
}

//返回日期时间格式为 年月日时分秒毫秒
const timeNum=() =>{
  let d = new Date();
  let date1 = d.getFullYear() + '' + ((d.getMonth() + 1) < 10 ? "0"+(d.getMonth() + 1): (d.getMonth() + 1))+'' +
      ((d.getDate()) < 10 ? "0"+(d.getDate()) : (d.getDate())) + '' +
      (d.getHours() < 10 ? "0"+d.getHours() : d.getHours())+ '' +
      (d.getMinutes() < 10 ? "0"+d.getMinutes() : d.getMinutes()) + '' +
      (d.getSeconds() < 10 ? "0"+d.getSeconds() : d.getSeconds())+ ''+
      (d.getMilliseconds());
  return date1;
}

//获取当前时间戳，毫秒位补0，补齐三位
const currentTimestamp=() =>{
  let now = moment(new Date());
  let milli = "000" + now.get('milliseconds');
  milli = milli.substring(milli.length - 3, milli.length);

  let year = now.get('year'),
      month = (now.get('month') + 1) < 10 ? "0"+(now.get('month') + 1) : now.get('month') + 1,
      date = (now.get('date'))<10 ? "0"+(now.get('date')) : now.get('date'),
      hour = (now.get('hour'))>10 ? "0"+(now.get('hour')) : now.get('hour'),
      minute = (now.get('minute'))<10 ? "0"+(now.get('minute')) : now.get('minute'),
      second = (now.get('second'))<10 ? "0"+(now.get('second')) : now.get('second');

  return "" + year + month + date + hour + minute + second + milli;
}

//数组去重去空
const arrayRepeatEmpty=(arr) =>{
  //去掉空值
  let arrNew=[];
  arr.forEach(item => {
    if(item){
      arrNew.push(item);
    }
  });

  //利用Set去重
  let arrSet=new Set(arrNew);
  arrNew=Array.from(arrSet);

  return arrNew;
}

//毫秒转成hh:mm:dd格式（传参：毫秒值，不是时间戳）
const numToTime=(ms) =>{
  let h=Math.floor(ms/1000/60/60);
  let m=Math.floor(ms/1000/60);
  let s=Math.floor(ms/1000);

  if(h==1){
    if(m/60==1 && s%60==0){
      h='';
      m='60:';
    }else{
      h='01:';
      m=m%10 + ':';
    }
    s=s%60;
  }else{
    if(h==0){
      h='';
    }else{
      h=h+':';
      m=m%60 + ':';
     //。。。没写完
    }
  }
}

/*
  数组对象排序
  调用格式：
  数组.sort(util.compareobjectArray('字段名',1或2));
*/
const compareobjectArray=(property,type) =>{
  return function(a,b){
    let value1 = a[property];
    let value2 = b[property];

    //升序
    if(type==1){
      return value1 - value2;
    }

    //降序
    if(type==2){
      return value2 - value1;
    }   
  }
}

module.exports = {
  formatTime: formatTime,
  timeFormat: timeFormat,
  timeFormatDay: timeFormatDay,
  timeFormatWeek: timeFormatWeek,
  countPrevDay: countPrevDay,
  secondToFormat: secondToFormat,
  timeFormatMonth: timeFormatMonth,
  timeFormatMonth2: timeFormatMonth2,
  beforeMonth: beforeMonth,
  checkPhoneFormat: checkPhoneFormat,
  uniqueConcat: uniqueConcat,
  countNextDay: countNextDay,
  timeFormatTZ: timeFormatTZ,
  timeFormatDayMin: timeFormatDayMin,
  timeFormatTime: timeFormatTime,
  timeNum: timeNum,
  currentTimestamp: currentTimestamp,
  arrayRepeatEmpty: arrayRepeatEmpty,
  compareobjectArray: compareobjectArray
}